<?php

namespace app\admin\model\activity;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'activity_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_status_text',
        'pay_time_text',
        'status_text'
    ];

    public function activity()
    {
        return $this->belongsTo('\\app\\admin\\model\\Activity', 'activity_id', 'id')->setEagerlyType(0);;
    }

    public function shop()
    {
        return $this->belongsTo('\\app\\admin\\model\\User', 'shop_id', 'id')->setEagerlyType(1);;
    }

    public function user()
    {
        return $this->belongsTo('\\app\\admin\\model\\User', 'buy_id', 'id')->setEagerlyType(1);;
    }

    public function dre()
    {
        return $this->belongsTo('\\app\\admin\\model\\User', 'dre_id', 'id')->setEagerlyType(1);;
    }


    
    public function getPayStatusList()
    {
        return ['1' => __('Pay_status 1'), '2' => __('Pay_status 2'), '3' => __('Pay_status 3')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
